package com.example.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.example.annotations.*;
import com.example.auth.model.DeptModel;
import com.example.auth.service.DeptModelService;
import com.example.db.model.IPage;
import com.example.model.TreeModel;
import com.example.utils.Results;

import java.sql.SQLOutput;
import java.util.Date;
import java.util.List;

/**
 * @Classname DeptModelController
 * @Description TODO
 * @Date 2022/7/31 0:21
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@RestController("/dept")
public class DeptModelController {

    @Autowired
    DeptModelService deptModelService;

    @PostMapping(value = "/page",desc = "分页")
    public JSONObject pageDeptModelController(@Param(value = "pageNum",desc = "页码",defalultValue = "1")Integer pageNum,
                                              @Param(value = "pageSize",desc = "条数",defalultValue = "10")Integer pageSize,
                                              @Param(value = "searchParams",required = false)JSONObject object){
        IPage<DeptModel> page=deptModelService.pageDeptModelIPage(pageNum,pageSize,object);
        return Results.successLayuiTable(page);
    }

    @GetMapping("/getone")
    public JSONObject getDeptModelByIdController(@Param(value = "id")String id){
        DeptModel deptModel=deptModelService.getDeptModelById(id);
        return Results.success(deptModel);
    }

    @PostMapping("/save")
    public JSONObject saveDeptModelController(@RequestBody DeptModel deptModel){
        if(deptModel==null){
            return Results.success("数据不得为空");
        }
        deptModel.setCreatedate(new Date());
        deptModelService.saveDeptModel(deptModel);
        return Results.success("新增成功");
    }

    @PostMapping("/update")
    public JSONObject updateDeptModelByIdController(@RequestBody DeptModel deptModel){
        if(deptModel==null){
            return Results.error("重要信息不得为空");
        }
        DeptModel model=deptModelService.getDeptModelById(deptModel.getId());
        if(model == null){
            return Results.error("部门信息不存在");
        }
        deptModelService.updateDeptModelById(deptModel);
        return Results.success("更新成功");
    }

    @GetMapping(value = "/getlist",desc = "获取所有")
    public JSONObject getDeptModelListController(){
        List<DeptModel> deptModels=deptModelService.getDeptModelList();
        return Results.success(deptModels);
    }

    @GetMapping(value = "/gettree",desc = "获取树形列表")
    public JSONObject getDeptModelTreeListController(){
        List<TreeModel> treeModels=deptModelService.getDeptModelTreeList();
        for (int i = 0; i < treeModels.size() ; i++) {
            System.out.println(JSONObject.toJSONString(treeModels.get(i)));
        }
        JSONObject object=new JSONObject();
        object.put("list",treeModels);
        String json=JSON.toJSONString(object, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject obj=JSONObject.parseObject(json);
        return Results.success(obj);
    }

    @Auth
    @GetMapping("/getchildlist")
    public JSONObject getChildListController(@Param("id")String id){
        List<DeptModel> deptModels=deptModelService.getDeptModelChildListById(id);
        return Results.success(deptModels);
    }
}
